package com.yk.faceapplication.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.allinpay.xed.R;
import com.android.volley.VolleyError;
import com.yk.faceapplication.callback.RequestCallback;
import com.yk.faceapplication.http.RequestUrl;
import com.yk.faceapplication.util.ActivityUtil;
import com.yk.faceapplication.util.RequestUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yk_2018/1/12_16:29
 */

public class RePwdActivity extends BaseActivity{
    private Button getCodeBtn,nextBtn;
    private EditText phoneET,codeET;
    private final int SEND_MSG_SUCCESS = 100;
    private final int MSG_TIME = 101;
    private int time = 60;
    private Dialog loadingDialog;


    @Override
    protected void onHandleMessage(Message msg) {
        super.onHandleMessage(msg);
        switch (msg.what){
            case SEND_MSG_SUCCESS:
                //                getCodeBtn.setEnabled(false);
                sendMessage(MSG_TIME);
                break;
            case MSG_TIME:
                if(time == 1){
                    time = 60;
                    getCodeBtn.setText("获取验证码");
                    getCodeBtn.setEnabled(true);
                }else{
                    getCodeBtn.setText(--time+"");
                    sendMessageDelayed(MSG_TIME,1000);
                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setTitle("找回密码");
        getCodeBtn = (Button) findViewById(R.id.get_code_btn);
        nextBtn = (Button) findViewById(R.id.next_btn);
        phoneET = (EditText) findViewById(R.id.phone_et);
        codeET = (EditText) findViewById(R.id.code_et);
        getCodeBtn.setOnClickListener(this);
        nextBtn.setOnClickListener(this);
        codeET.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 输入的内容变化的监听
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // 输入前的监听

            }

            @Override
            public void afterTextChanged(Editable s) {
                // 输入后的监听
                checkInputInfo();
            }
        });
        phoneET.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 输入的内容变化的监听
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // 输入前的监听

            }

            @Override
            public void afterTextChanged(Editable s) {
                // 输入后的监听
                checkInputInfo();
            }
        });
        checkInputInfo();
    }

    private void checkInputInfo(){
        if(ActivityUtil.editIsNull(phoneET) || ActivityUtil.editIsNull(codeET)){
            nextBtn.setBackgroundColor(getResources().getColor(R.color.bg_gray));
            nextBtn.setEnabled(false);
        }else{
            nextBtn.setBackgroundColor(getResources().getColor(R.color.textRegisterFile));
            nextBtn.setEnabled(true);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch(v.getId()){
            case R.id.get_code_btn:
                if(!ActivityUtil.isMobileNO(phoneET.getText().toString())){
                    Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                getCode();
                break;
            case R.id.next_btn:
                rePwd();
                break;
        }
    }

    private final int REQUEST_NEXT = 1000;

    //    忘记密码
    private void rePwd(){
        loadingDialog = ActivityUtil.createLoadingDialog(this, "请求中,请稍候~");
        loadingDialog.show();
        Map<String, String> form = new HashMap<String, String>();
        form.put("mobile", phoneET.getText().toString());
        form.put("code", codeET.getText().toString());
        RequestCallback callback = new RequestCallback() {
            @Override
            public void success(JSONObject jsonObject) {
                loadingDialog.dismiss();
                Intent intent = new Intent(RePwdActivity.this,SetPwdActivity.class);
                intent.putExtra("mobile",phoneET.getText().toString());
                startActivityForResult(intent,REQUEST_NEXT);
            }

            @Override
            public void fail(VolleyError volleyError) {
                loadingDialog.dismiss();
            }
        };
        RequestUtil.request(this,RequestUrl.REPWD,callback,form);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUEST_NEXT){
            if(resultCode == RESULT_OK){
                finishAct();
            }
        }
    }

    //    获取验证码
    private void getCode(){
        getCodeBtn.setEnabled(false);
        Map<String, String> form = new HashMap<String, String>();
        form.put("flag", "1");
        form.put("mobile", phoneET.getText().toString());
        RequestCallback callback = new RequestCallback() {
            @Override
            public void success(JSONObject jsonObject) {
                Toast.makeText(RePwdActivity.this, "验证码发送成功",Toast.LENGTH_SHORT).show();
                sendMessage(SEND_MSG_SUCCESS);
            }

            @Override
            public void fail(VolleyError volleyError) {
                getCodeBtn.setEnabled(true);
            }
        };
        RequestUtil.request(this,RequestUrl.SEND_MSG,callback,form);
    }

}
